﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;

namespace RooHui.Infrastructure.Entities.Repository
{
    [Table("P_AmountHistory")]
    public class P_AmountHistory : IEntity<P_AmountHistory>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string UserName { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string TransactionUser { get; set; }
        public int OperationType { get; set; }
        public decimal ChangeValue { get; set; }
        public decimal PreAmount { get; set; }
        public decimal CurrentAmount { get; set; }
        public decimal PreFreeze { get; set; }
        public decimal CurrentFreeze { get; set; }
        public DateTime CreateTime { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string Remark { get; set; }
        public int AmountOperationID { get; set; }

        public override P_AmountHistory Find(string key)
        {
            int Id = Int32.Parse(key);
            return Find(c => c.ID == Id);
        }

        public override P_AmountHistory Find(System.Linq.Expressions.Expression<Func<P_AmountHistory, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.AmountHistory.FirstOrDefault(where);
                if (item != null)
                {
                    this.AmountOperationID = item.AmountOperationID;
                    this.ChangeValue = item.ChangeValue;
                    this.CreateTime = item.CreateTime;
                    this.CurrentAmount = item.CurrentAmount;
                    this.CurrentFreeze = item.CurrentFreeze;
                    this.ID = item.ID;
                    this.OperationType = item.OperationType;
                    this.PreAmount = item.PreAmount;
                    this.PreFreeze = item.PreFreeze;
                    this.Remark = item.Remark;
                    this.TransactionUser = item.TransactionUser;
                    this.UserName = item.UserName;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<P_AmountHistory, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.AmountHistory.Any(where);
            }
        }
    }
}